<?php

namespace frontend\modules\business\controllers;

use yii;
use yii\web\Controller;
use yii\data\Pagination;

/**
 * Default controller for the `business` module
 */
class BusinessController extends BaseController
{
    public $business = [];
    
    public $master = null;
    
    public $businessDB = null;
    
    public function init()
    {
        parent::init();

        $this->enableCsrfValidation = false;
        $this->business = $this->module->params['business'];
        
        $admin_id = Yii::$app->request->get('admin_id',0);

        if (intval($admin_id)) {
            
            if (array_key_exists($admin_id, $this->business)) {
                
                $host = $this->business[$admin_id]['host'];
                $dbname = isset($this->business[$admin_id]['dbname']) ? $this->business[$admin_id]['dbname'] : 'xddb';
                $username = isset($this->business[$admin_id]['username']) ? $this->business[$admin_id]['username'] : 'dladmin';
                $password = isset($this->business[$admin_id]['password']) ? $this->business[$admin_id]['password'] : 'root@123$';

                $this->businessDB = new yii\db\Connection([
                    'dsn' => "mysql:host={$host};dbname={$dbname}",
                    'username' => $username,
                    'password' => $password,
                    'charset' => 'utf8',
                    'tablePrefix' => 'busy_',
                ]);
   
            }
 
        }
        
    }
    
    /**
     * 查询对应的数据
     */
    public function actionMember()
    {
        if (!$this->businessDB) {
            exit('business db not setting! \n');
        }

        if (!Yii::$app->request->isPost) {
            exit('request methoed error! \n');
        }
        
        $user_id = Yii::$app->request->post('user_id');
        $user_phone = Yii::$app->request->post('user_phone');
        
        if (intval($user_id) == 0 && $user_phone == '') {
            exit('request params error! \n');
        }
        
        $where = [];
        
        if ($user_id) {
            $where['a.id'] = $user_id;
        }
        
        if ($user_phone) {
            $where['a.phone'] = $user_phone;
        }
        
        $users = (new \yii\db\Query())
        ->select(['a.id','a.sex','a.identity','a.type','a.del_status','a.is_payed','a.type_info','a.initial_amount','a.is_auto_examine','a.ruletype','a.can_amount','a.sid','a.admin_id','a.auditor_id','a.age','a.is_trial','a.phone','a.s_create_time','c.username','d.yun_type','d.yun_token','d.big_status'])
        ->from('busy_member as a')
        ->leftJoin(['b' => 'busy_member_base'], 'b.id = a.id')
        ->leftJoin(['c' => 'busy_admin_user'], 'c.id = a.admin_id')
        ->leftJoin(['d' => 'busy_member_info'], 'd.id = a.id')
        ->where($where)
        ->all($this->businessDB);

        return $this->render('@module/business/views/member/show.php', ['users'=>$users]);

    }
    
    /**
     * 渠道推广
     * @return string
     */
    public function actionChannels()
    {
        $admin_id = Yii::$app->request->get('admin_id');
        
        if (intval($admin_id) == 0) {
            $this->redirect(['index']);
        }
        
        $business = $this->getBusinessInfo($admin_id);
        
        $list = (new \yii\db\Query())
        ->from('busy_admin_link_share')
        ->where(['userid'=>$admin_id])
        ->all($this->master);
        
        return $this->render('channels', ['list'=>$list, 'business'=>$business]);
    }
    
    /**
     * 收费项目列表
     */
    public function actionPricedList()
    {
        $list = (new \yii\db\Query())
        ->select('*')
        ->from('busy_consume_price')
        ->all($this->master);

        $models = (new \yii\db\Query())->select('*')->from('busy_sms_model')->all($this->master);
        return $this->render('pricedList', ['list'=>$list, 'models'=>$models]);  
    }
    
    

}
